package com.sysmanage.permissions.service.impl;


import java.util.ArrayList;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.sysmanage.common.tools.json.Dic;
import com.sysmanage.common.tools.util.ArgumentMemoryUtils;
import com.sysmanage.permissions.ConstCode;
import com.sysmanage.permissions.dao.CompanyDao;
import com.sysmanage.permissions.dao.UsersDao;
import com.sysmanage.permissions.model.Company;
import com.sysmanage.permissions.model.Users;
import com.sysmanage.permissions.service.IUserDataRightService;

@Service(value="userDataRightService")
public class UserDataRightService  implements IUserDataRightService {
	
	@Resource(name="userDao")
	private UsersDao userDao;
	
	@Resource(name="companyDao")
	private CompanyDao companyDao;

	@Override
	public Integer getUserCompanyId(Integer userId) {
		Users user = userDao.findById(userId);
		if(user!=null)
		{
			return user.getCompanyId();
		}
		return null;
	}
	private List<Company> getUserCompanys(Integer userId) {
		List<Company> canLooks = new ArrayList<Company>();
		Users user = userDao.findById(userId);
		if(user!=null&&user.getCompanyId()!=null){
			
			String strRootCompanyId = ArgumentMemoryUtils.getInstance().getValueByName("ROOT_COMPANY_ID","1");
			Integer rootCompanyId = 1;
			//如果是总部人员，则直接可以查看所有子公司数据
			try{
				rootCompanyId = Integer.parseInt(strRootCompanyId);
			}catch(Exception ex)
			{
				rootCompanyId = 1;
			}
			//如果是总部人员
			if(rootCompanyId==user.getCompanyId())
			{
				canLooks.addAll(companyDao.getAll());
			}
			else
			{
				Company item = companyDao.findById(user.getCompanyId());
				if(item!=null)
				{
					canLooks.add(item);
					String limit = ArgumentMemoryUtils.getInstance().getValueByName("LIMIT_SUB_COMPANY_DATA");
					if(!ConstCode.LIMIT_SUB_COMPANY_DATA_YES.equals(limit))
					{
						//如果不限制
						canLooks.addAll(companyDao.getAllSubCompany(user.getCompanyId()));
					}
				}
			}
		}
		/*
		if(canLooks.size()==0)
		{
			canLooks.add(new Company(-1));
		}
		*/
		return canLooks;
	}
	@Override
	public List<Integer> getUserCompanyIds(Integer userId) {
		//首先查看数据该用户的数据权限
		//目前所有客户经理只能看见本店数据，其它人可以看见其所在公司下的子公司数据，
		//总公司能看见所有公司数据
		//TODO 以后将增加角色的数据权限，来限制每个角色能看到的数据
		List<Integer> companyList = new ArrayList<Integer>();
		List<Company> canLooks = this.getUserCompanys(userId);
		for(Company item:canLooks)
		{
			companyList.add(item.getId());
		}
		return companyList;
	}

	@Override
	public List<Dic> getUserCompanyDics(Integer userId,boolean addAllOption) {
		List<Dic> companyList = new ArrayList<Dic>();
		List<Company> canLooks = this.getUserCompanys(userId);
		for(Company item:canLooks)
		{
			Dic dic = new Dic(item.getId()+"",item.getCompanyName());
			companyList.add(dic);
		}
		//如果包含多个，添加一个选项，所有
		if(addAllOption&&companyList.size()>1)
		{
			companyList.add(0,new Dic("-1","所有"));
		}
		return companyList;
	}
	/**
	 * @description 根据用户ID来获取该用户所能看见的数据范围（店）字符串（用,分割）
	 * @param userId
	 * @return
	 */
	public String getStrUserCompanyIds(Integer userId){
		String ids = null;
		List<Company> canLooks = this.getUserCompanys(userId);
		for(Company item:canLooks)
		{	
			if(ids==null)
			{
				ids = item.getId()+"";
			}
			else
			{
				ids = ids + "," + item.getId();
			}
			
		}
		return ids;
	}
	/**
	 * @description 根据用户ID来获取该店的客户经理字典列表
	 * @param ShopId
	 * @return
	 */
	public List<Dic> getCustManagerDics(Integer ShopId,boolean judgeIsManager){
		List<Dic> custManagerList = new ArrayList<Dic>();
		List<Users> usersList = userDao.findByCompanyId(ShopId);
		if(usersList!=null&&usersList.size()>0)
		{
			for(int i=0;i<usersList.size();i++)
			{
				Users item = usersList.get(i);
				if(!(judgeIsManager&&(item.getIsSa()==null||ConstCode.YESORNO_YES!=item.getIsSa())))
				{
					custManagerList.add(new Dic(item.getId()+"",item.getName()));
				}
			}
		}
		
		return custManagerList;
	}
	/**
	 * @description 根据用户所在的店id去查询出该店的所有的用户
	 * @param ShopId
	 * @return
	 */
	public List<Dic> getUsersDics(Integer ShopId){
		List<Dic> users = new ArrayList<Dic>();
		List<Users> usersList = userDao.findByCompanyId(ShopId);
		if(usersList!=null&&usersList.size()>0)
		{
			for(int i=0;i<usersList.size();i++)
			{
				Users item = usersList.get(i);
				
				users.add(new Dic(item.getId()+"",item.getName()));
				
			}
		}
		
		return users;
	}
}
